<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

require('ext_lib/magpierss/rss_fetch.inc');
include_once("ext_lib/simple_html_dom.php");


function execute_rss($site_content){
    global $lang;
    $html = str_get_html($site_content);

    // look for rss extension: ekkin_rss("$url")
    //echo strrpos($site_content, "ekkin_rss");


    foreach($html->find("a") as $element){
        if ($element->class == "ekkin"){
            if ($element->id == "rss"){
                $source_lang = $element->hreflang;
                $rss_content = generate_rss($element->innertext, $items=5, $target_lang=$lang, $source_lang=$source_lang);
                $element->outertext = "<p>$rss_content</p>";
                //$element->href = "";
            }

        }
    }

    return $html->save();
}


function generate_rss($url, $items=5, $target_lang="en", $source_lang="es"){
    $rss = fetch_rss($url);
    $all = "<div class=\"rss-box\">";
    $items = array_slice($rss->items, 0, $items);
    $max = 3;
    $counter = 0;
    foreach($items as $n => $entry){

        if ($counter == $max ){
            break;
        }
        //print_r($entry);
        $content = $entry["atom_content"];

        $n_chars = strlen($content);
        if ($n_chars > 1000){
            $a = str_split($content);
            $c = 0;
            $b = array();
            foreach($a as $char){
                $c ++;


                if ($c >= 1000){
                    $b[$c] = "...";
                    break;
                } else {
                    $b[$c] = $char;
                }
            }
            $content2 = implode($b);

        }else {
            $content2 = $content;
        }

        $title = $entry["title"];

        if ($target_lang != $source_lang and $source_lang != ""){
            require_once('ext_lib/googleTranslate/googleTranslate.class.php');
            $gt = new GoogleTranslateWrapper();
            $content2 = $gt->translate("$content2", $target_lang, $source_lang);
            $content2 .= "<font size=1>Translation from $source_lang to $target_lang by Google</font>";
            $gt = new GoogleTranslateWrapper();
            $title = $gt->translate("$title", $target_lang, $source_lang);
        }

        $all .= "<div style=\"border-bottom: 1px solid #BBBBBB;\" class=\"rss-content\"><img align=\"left\" height=\"20\" src=\"templates/pronaturatours/images/blog_icon.png\"/><h3>".$title."</h3>\n";
        $all .= "".$content2."";
        $all .= "<br/></div>";
        //$all .= "<br/><a href=\"".$entry["link"]."\">Read full story ... </a></div>";


        $counter ++;

    }
    $all .= "</div>";
    return $all;
}

?>
